﻿using fuzhu;
using LH.Model.PU;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Newtonsoft.Json;
using Org.BouncyCastle.Ocsp;
using RestSharp;
using SqlSuarEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XXX.Common;

namespace XXX.Bo
{
    public static class JsonSaveService
    {
        /// <summary>
        /// 保存json到后台数据库,没有返回值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cid">主键</param> 
        /// <param name="cType">业务类型</param> 
        /// <param name="model">传入的模型，取单据号和日期</param> 
        /// <param name="iSavelog">是否保存</param>
        public static void saveLog<T>(T model,string cType,string json,int istate, string cMemo)
        {
            try
            {
                 string cid = Guid.NewGuid().ToString();
                List<Zdy_APILog> lst = new List<Zdy_APILog>();
                Zdy_APILog rd = new Zdy_APILog();
                rd.id = cid;
                rd.cType = cType;
                //if (model is HuaJinRdrecord huaJinRdrecord)
                //{
                //    rd.cCode = huaJinRdrecord.cCode;
                //    rd.dDate = DbHelper.GetDbDate(huaJinRdrecord.dDate);
                //}
                //else if (model is InventoryStatus inventoryStatus)
                //{                    
                //    rd.cCode = inventoryStatus.cInvCode;
                //    rd.dDate = DateTime.Now;
                //}
                //else if (model is HuaJinRdVerify huaJinRdVerify)
                //{                  
                //    rd.cCode = huaJinRdVerify.cCode;
                //    rd.dDate =DbHelper.GetDbDate(huaJinRdVerify.dVeriDate);
                //}                
                //else if (model is HuaJinTransVouch huaJinTranVouch)
                //{                  
                //    rd.cCode = huaJinTranVouch.cTVCode;
                //    rd.dDate = DbHelper.GetDbDate(huaJinTranVouch.dDate);
                //}
                //else if (model is HuaJinMorder huaJinMorder)
                //{
                //    rd.cCode = huaJinMorder.cMoCode;
                rd.dDate = DateTime.Now.Date;
                //}
                ////else
                //{
                //    LoggerHelper logger = new LoggerHelper();
                //    logger.Error("接口保存","未定义的实体类型：" + model.GetType().Name);
                //    //没有定义的，不保存
                    
                //}
                rd.cText = json;
                rd.dTime = System.DateTime.Now;
                rd.iState = istate;
                rd.cMemo = cMemo;
                lst.Add(rd);
                
                    var cnt = SqlsugarHelper.Instance.Insertable(lst) //插入1层 Root->Books
        .ExecuteCommand();
                    return;
              
            }
            catch (Exception ex)
            {
                LoggerHelper logger = new LoggerHelper();
                logger.Error("接口保存", ex.Message);
                return;
            }
            
        }



        /// <summary>
        /// 推送MES 的保存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cid">主键</param> 
        /// <param name="cType">业务类型</param> 
        /// <param name="model">传入的模型，取单据号和日期</param> 
        /// <param name="iSavelog">是否保存</param>
        public static void saveLog2(string cType, string ccode,string json, int istate, string cMemo)
        {
            try
            {
                string cid = Guid.NewGuid().ToString();
                List<Zdy_APILog> lst = new List<Zdy_APILog>();
                Zdy_APILog rd = new Zdy_APILog();
                rd.id = cid;
                rd.cType = cType;
                rd.cCode = ccode;
                rd.cText = json;
                rd.dDate = DateTime.Now.Date;
                rd.dTime = System.DateTime.Now;
                rd.iState = istate;
                rd.cMemo = cMemo;
                lst.Add(rd);

                
                var cnt = SqlsugarHelper.Instance.Insertable(lst) //插入1层 Root->Books
    .ExecuteCommand();
                return;

            }
            catch (Exception ex)
            {
                LoggerHelper logger = new LoggerHelper();
                logger.Error("接口保存", ex.Message);
                return;
            }

        }

        /// <summary>
        /// 推送MES 的保存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cid">主键</param> 
        /// <param name="cType">业务类型</param> 
        /// <param name="model">传入的模型，取单据号和日期</param> 
        /// <param name="iSavelog">是否保存</param>
        public static void saveLog2Asyn(string cType, string ccode, string json, int istate, string cMemo)
        {
            try
            {
                string cid = Guid.NewGuid().ToString();
                List<Zdy_APILog> lst = new List<Zdy_APILog>();
                Zdy_APILog rd = new Zdy_APILog();
                rd.id = cid;
                rd.cType = cType;
                rd.cCode = ccode;
                rd.cText = json;
                rd.dDate = DateTime.Now.Date;
                rd.dTime = System.DateTime.Now;
                rd.iState = istate;
                rd.cMemo = cMemo;
                lst.Add(rd);


                var cnt = SqlsugarHelper.Instance.Insertable(lst) //插入1层 Root->Books
    .ExecuteCommandAsync();
                return;

            }
            catch (Exception ex)
            {
                LoggerHelper logger = new LoggerHelper();
                logger.Error("接口保存", ex.Message);
                return;
            }

        }
    }
}

